home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
smaltalk
/
manchest.lha
/
MANCHESTER
/
manchester
/
2.2
/
Form-features.st
< prev
next >
Wrap
Text File
|
1993-07-24
|
6KB
|
160 lines
" NAME Form-shrinkBy
AUTHOR Dr Who
FUNCTION feature extraction --- contrast improvement
ST-VERSIONS
PREREQUISITES
CONFLICTS
DISTRIBUTION world
VERSION 1.1
DATE 22 Jan 1989
SUMMARY
This method performs a simple feature extraction algorithm. Each pixel in
a form (the receiver) has each of the surrounding pixels (plus itself) summed
to return a value between 0 and 9. If the resulting value is between
the bounds given (lowerBound and UpperBound), then
the pixel is set black (1), otherwise the pixel is set white (0). For performance
reasons, the arithmetic is done with BitBlt operations (as in the Game of Life
implementation)."
'From Smalltalk-80, version 2, of April 1, 1983 on 4 January 1987 at 11:04:49 am'!
!Form methodsFor: 'features'!
edgesAt: aValue
"This method performs a simple feature extraction algorithm. Each pixel in
a form (the receiver) has each of the surrounding pixels (plus itself) summed
to return a value between 0 and 9. If this value is equal to aValue,
the pixel is set black (1), otherwise the pixel is set white (0). This version is
more efficient than the general method edgesFrom: <lowerBound> to: <upperbound>."
"Form fromUser edgesAt: 3; display."
| nbr1 nbr2 nbr4 nbr8 carry2 carry4 carry8 all delta |
nbr1 _ Form extent: self extent.
nbr2 _ Form extent: self extent.
nbr4 _ Form extent: self extent.
nbr8 _ Form extent: self extent.
carry2 _ Form extent: self extent.
carry4 _ Form extent: self extent.
carry8 _ Form extent: self extent.
all _ self boundingBox.
1 to: 9 do:
[:i |
"delta is the offset of the eight neighboring cells, plus self."
delta _
((#(-1 0 1 1 1 0 -1 -1 0) at: i) @ (#(-1 -1 -1 0 1 1 1 0 0) at: i)).
carry2 copy: all from: 0@0 in: nbr1 rule: 3.
carry2 copy: all from: delta in: self rule: 1. "AND for carry into 2"
nbr1 copy: all from: delta in: self rule: 6. "XOR for sum 1"
carry4 copy: all from: 0@0 in: nbr2 rule: 3.
carry4 copy: all from: 0@0 in: carry2 rule: 1. "AND for carry into 4"
nbr2 copy: all from: 0@0 in: carry2 rule: 6. "XOR for sum 2"
carry8 copy: all from: 0@0 in: nbr4 rule: 3.
carry8 copy: all from: 0@0 in: carry4 rule: 1. "AND for carry into 8"
nbr4 copy: all from: 0@0 in: carry4 rule: 6. "XOR for sum 4"
nbr8 copy: all from: 0@0 in: carry8 rule: 6]. "XOR for sum 8 (ignore carry)"
aValue even ifTrue: [nbr1 reverse].
self copy: all from: 0@0 in: nbr1 rule: 3. "Overwrite receiver for lsb."
(aValue // 2) even ifTrue: [nbr2 reverse].
self copy: all from: 0@0 in: nbr2 rule: 1. "AND for subsequent bits."
(aValue // 4) even ifTrue: [nbr4 reverse].
self copy: all from: 0@0 in: nbr4 rule: 1.
(aValue // 8) even ifTrue: [nbr8 reverse].
self copy: all from: 0@0 in: nbr8 rule: 1.!
edgesDownTo: aValue
"Performs edge extraction with bounds aValue and 9."
aValue = 9
ifTrue: [self edgesAt: 9]
ifFalse: [self edgesFrom: aValue to: 9]
"Form fromUser edgesDownTo: 2; display."!
edgesFrom: lowerBound to: upperBound
"This method performs a simple feature extraction algorithm. Each pixel in
a form (the receiver) has each of the surrounding pixels (plus itself) summed
to return a value between 0 and 9. If the resulting value is between
the bounds given (lowerBound and UpperBound), then
the pixel is set black (1), otherwise the pixel is set white (0). For performance
reasons, the arithmetic is done with BitBlt operations (as in the Game of Life
implementation)."
"Form fromUser edgesFrom: 2 to: 4; display."
| nbr1 nbr2 nbr4 nbr8
carry2 carry4 carry8 all delta aValue tempForm |
nbr1 _ Form extent: self extent.
nbr2 _ Form extent: self extent.
nbr4 _ Form extent: self extent.
nbr8 _ Form extent: self extent.
carry2 _ Form extent: self extent.
carry4 _ Form extent: self extent.
carry8 _ Form extent: self extent.
all _ self boundingBox.
1 to: 9 do:
[:i |
"delta is the offset of the eight neighboring cells, plus self."
delta _
((#(-1 0 1 1 1 0 -1 -1 0) at: i) @ (#(-1 -1 -1 0 1 1 1 0 0) at: i)).
carry2 copy: all from: 0@0 in: nbr1 rule: 3.
carry2 copy: all from: delta in: self rule: 1. "AND for carry into 2"
nbr1 copy: all from: delta in: self rule: 6. "XOR for sum 1"
carry4 copy: all from: 0@0 in: nbr2 rule: 3.
carry4 copy: all from: 0@0 in: carry2 rule: 1. "AND for carry into 4"
nbr2 copy: all from: 0@0 in: carry2 rule: 6. "XOR for sum 2"
carry8 copy: all from: 0@0 in: nbr4 rule: 3.
carry8 copy: all from: 0@0 in: carry4 rule: 1. "AND for carry into 8"
nbr4 copy: all from: 0@0 in: carry4 rule: 6. "XOR for sum 4"
nbr8 copy: all from: 0@0 in: carry8 rule: 6]. "XOR for sum 8 (ignore carry)"
carry2 _ nbr2 deepCopy reverse. "Make reversed copies."
carry4 _ nbr4 deepCopy reverse.
carry8 _ nbr8 deepCopy reverse.
self copy: all from: 0@0 in: self rule: 6. "Clear receiver form."
lowerBound to: upperBound do: [ :aValue |
tempForm _ Form extent: self extent.
aValue even
ifTrue: [tempForm copy: all from: 0@0 in: nbr1 rule: 12.]
ifFalse: [tempForm copy: all from: 0@0 in: nbr1 rule: 3].
"Overwrite receiver for lsb."
(aValue // 2) even
ifTrue: [tempForm copy: all from: 0@0 in: carry2 rule: 1]
ifFalse: [tempForm copy: all from: 0@0 in: nbr2 rule: 1].
"AND for subsequent bits."
(aValue // 4) even
ifTrue: [tempForm copy: all from: 0@0 in: carry4 rule: 1]
ifFalse: [tempForm copy: all from: 0@0 in: nbr4 rule: 1].
(aValue // 8) even
ifTrue: [tempForm copy: all from: 0@0 in: carry8 rule: 1]
ifFalse: [tempForm copy: all from: 0@0 in: nbr8 rule: 1].
self copy: all from: 0@0 in: tempForm rule: 7. "Combine with OR."
].!
edgesUpTo: aValue
"Performs edge extraction with bounds 0 and aValue."
aValue = 0
ifTrue: [self edgesAt: 0]
ifFalse: [self edgesFrom: 0 to: aValue]
"Form fromUser edgesUpTo: 2; display."! !